<template>
  <div class="goods-edit">
    <div class="goods-content">
      <el-form ref="goodFormRef" :label-width="110" :status-icon="true" :show-message="false">
        <div class="goods_info">
          <p class="title">商品信息</p>
          <el-form-item label="商品名称" required>
            <el-input style="width: 600px" v-model="goodsData.goodsName"></el-input>
          </el-form-item>
          <el-form-item label="商品规格" required>
            <el-input style="width: 600px" v-model="goodsData.goodsSpec"></el-input>
          </el-form-item>
          <el-form-item label="商品厂家" required>
            <el-input style="width: 600px" v-model="goodsData.factoryName"></el-input>
          </el-form-item>

          <el-form-item label="商品图片" required>
            <el-upload class="avatar-uploader" :headers="header" :action="actionUrl" :show-file-list="false"
                       :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
              <img v-if="goodsData.goodsImg" :src="goodsData.goodsImg" class="avatar" />
              <el-icon v-else class="avatar-uploader-icon">
                <Plus />
              </el-icon>
            </el-upload>
          </el-form-item>
        </div>
      </el-form>
    </div>
    <div class="audit_button">
      <el-button @click="onCancelClick">取消</el-button>
      <el-button @click='onSaveClick' type="primary">保存</el-button>
    </div>
  </div>
</template>
<script setup>
import { onMounted, reactive } from 'vue'
import { ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Plus, Minus } from '@element-plus/icons-vue'
import { apiBasePath } from '@/api/config'
import api from '@/api'
import router from '@/router'

const header = {
  'Authorization': `Bearer ${localStorage.getItem('_antiFake_token')}`
}

const actionUrl = apiBasePath + '/ruikeli/antiFake/uploadSingleFile'
const goodsData = reactive({
  goodsName: '',
  goodsSpec: '',
  goodsImg: '',
  factoryName: ''
})

const handleAvatarSuccess = (response, uploadFile) => {
  goodsData.goodsImg = response.data
}

const beforeAvatarUpload = (file) => {
  const isJPG = file.type === 'image/jpg' || file.type === 'image/png' || file.type === 'image/jpeg'
  const isLt1M = file.size / 1024 / 1024 < 10

  if (!isJPG) {
    ElMessage.error('只能上传jpg/png/jpeg文件!')
  }
  if (!isLt1M) {
    ElMessage.error('上传图片大小不能超过10MB!')
  }
  return isJPG && isLt1M
}


const onCancelClick = () => {
  restFun()
  router.push({ path: '/overview' })
}
const onSaveClick = () => {
  const url = '/ruikeli/antiFake/goods/addGoods'
  const isText = textFun()
  if (!isText) {
    return
  }
  ElMessageBox.confirm(
    '确定添加吗?',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  )
    .then(async () => {
      const { data, err } = await api.post(url, goodsData)
      if (err) {
        ElMessage.error(err)
      } else {
        ElMessage.success('新增成功')
        router.push({ path: '/overview' })
        restFun()
      }
    })
    .catch(() => {
    })
}
const restFun = () => {
  goodsData.goodsName = ''
  goodsData.goodsSpec = ''
  goodsData.goodsImg = ''
  goodsData.factoryName = ''
}

const textFun = () => {
  if (!goodsData.goodsName) {
    ElMessage.error('请输入商品名称')
    return false
  }
  if (!goodsData.goodsSpec) {
    ElMessage.error('请输入商品规格')
    return false
  }
  if (!goodsData.goodsImg) {
    ElMessage.error('请上传商品图片')
    return false
  }
  if (!goodsData.factoryName) {
    ElMessage.error('请输入商品厂家')
    return false
  }
  return true
}
onMounted(() => {

})
</script>
<style scoped lang="scss" *>
.goods-edit {
  .goods-content {
    margin: 0 15px 0 10px;

    .uploadImg {
      display: flex;
      align-items: flex-end;
      font-size: 12px;

      .icon-box {
        display: flex;
        flex-direction: column;
        align-items: center;

        .text {
          line-height: 14px;
          text-align: center;
          padding: 0 30px;
          margin-top: 8px;
          color: var(--color-text);
        }
      }

      .tips {
        .img-tip {
          color: var(--span-title-text);
          margin-left: 12px;
        }
      }
    }

    .boss_input,
    :deep(.el-cascader) {
      width: 750px;
    }

    :deep(.el-cascader) {
      .el-input {
        height: 40px;
      }
    }

    .goods_img {
      .el-upload--picture-card {
        width: 146px;
        height: 146px;
      }

      .el-upload-list {
        flex-wrap: nowrap !important;
      }
    }

    .goods_info,
    .goods_introduce,
    .goods_detail,
    .goods_license,
    .product_operation {
      background-color: var(--background);
      margin: 10px 0;
      padding: 20px;

      .title {
        height: 16px;
        line-height: 16px;
        font-size: 16px;
        font-weight: 500;
        margin-bottom: 20px;
      }
    }

    .goods_info {
      .goods_select,
      .expand {
        width: 750px;
      }

      .expand {
        display: flex;
        flex-direction: column;

        .expand-box {
          display: flex;

          .tag-label {
            position: relative;
            width: 36px;
            color: var(--span-title-text);
            font-weight: 500;
            padding-right: 8px;
          }

          .tag-label::after {
            content: '';
            width: 0;
            height: 0;
            position: absolute;
            left: 44px;
            top: 12px;
            border-top: 5px solid transparent;
            border-bottom: 5px solid transparent;
            border-left: 5px solid #919398;
          }

          .tags {
            margin-left: 18px;
            flex: 1;
          }
        }
      }
    }

    .goods_introduce,
    .goods_info {
      .items {
        display: flex;

        ._left,
        ._right {
          display: flex;
          flex-direction: column;

          .boss_input {
            width: 309px;
          }
        }

        ._right {
          margin-left: 20px;
        }
      }
    }

    .goods_introduce {
      .info {
        .product {
          display: flex;
          flex-direction: column;

          .uploadImg {
            margin-bottom: 20px;
          }
        }
      }
    }
  }
}

.goods_license {

  .type {
    .type-box {
      border: 1px solid var(--input-border-color);
      margin-bottom: 20px;
      border-radius: 4px;
    }

    .checkBox-small {
      display: flex;
      align-items: center;
      background-color: #fafafa;
      height: 48px;
      line-height: 16px;
      padding-left: 20px;
      border-radius: 4px;
    }

    .checkBox-large {
      .qulification {
        color: var(--color-text);
        padding-left: 10px;

        .el-form-item {
          margin: 0;
        }

        .item {
          margin: 20px 0;

          .upload-img {
            display: flex;
            align-items: flex-end;

            .tips {
              display: flex;
              align-items: center;

              .btn {
                margin-left: 10px;
              }

              .img-tip {
                font-size: 12px;
                color: var(--span-color-text);
                height: 16px;
                line-height: 16px;
                margin-left: 12px;
              }
            }

            .noImg {
              display: flex;
              flex-direction: column;
              align-items: center;
              justify-content: center;
              width: 80px;
              height: 80px;

              .el-icon svg {
                width: 25px;
                height: 25px;
                margin-bottom: 10px;
              }

              .tip {
                font-size: 12px;
                line-height: 12px;
                padding: 0 5px;
                text-align: center;
              }
            }
          }
        }

        .time {
          margin-bottom: 30px;

          .time-icon {
            font-size: 8px;
            margin: 0 11px;
            color: var(--span-color-text);
          }
        }
      }
    }
  }
}

.el-icon.avatar-uploader-icon {
  height: auto;
}

.disabledImg {
  :deep(.el-upload--picture-card) {
    display: none;
  }
}
</style>

<style scoped>
.goods_info .expand .el-form-item .el-form-item__content {
  margin-left: 0 !important;
}

.goods_info .el-radio__input {
  display: none;
}

.goods_info .el-radio__label {
  padding-left: 0;
  line-height: 18px;
}

.goods_info .el-radio.is-bordered {
  padding: 10px;
  height: 40px;
  margin: 0 10px 10px 0;
}

.tags .el-radio {
  position: relative;
}

.tags .el-radio.active .el-radio__label::after {
  content: '';
  position: absolute;
  right: 0;
  bottom: 0;
  width: 0;
  height: 0;
  border: 10px solid #0486fe;
  border-left-color: transparent;
  border-top-color: transparent;
}

.tags .el-radio.active .el-radio__label::before {
  content: '';
  position: absolute;
  bottom: 3px;
  right: 3px;
  width: 2px;
  height: 4px;
  border: 1px solid #fff;
  border-top-color: transparent;
  border-left-color: transparent;
  transform: rotate(45deg);
  z-index: 1;
}

.avatar-uploader .avatar {
  width: 146px;
  height: 146px;
  display: block;
}

.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 146px;
  height: 146px;
  text-align: center;
}
</style>
